/**
 * Created by zhangxiaoyu on 16/12/01.
 */
angular.module('hyt', [
  'ionic', 'ngCordova', 'hyt.route', 'hyt.config','hyt.ngCordovaMocksHdoit',
  'hyt.addWaybillCtrl', 'hyt.myOrderCtrl', 'hyt.outerCtrl', 'hyt.receiptCtrl', 'hyt.signCtrl', 'hyt.myWaybillCtrl', 'hyt.abnormalCtrl','hyt.loginCtrl','hyt.personCtrl','hyt.toolsCtrl','hyt.workbenchCtrl',
  'hyt.loginService','hyt.addWaybillService','hyt.myWaybillService','hyt.myOrderService','hyt.receiptService','hyt.personService','hyt.signService','hyt.outerService','hyt.abnormalService','hyt.toolsService','hyt.workbenchService'
])
  .run(function($ionicPlatform,$state,$location,$rootScope,$cordovaToast,$timeout,$ionicHistory,Storage,ValidLogin,$cordovaGeolocation) {
    $ionicPlatform.ready(function() {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        cordova.plugins.Keyboard.disableScroll(true);

      }
      if (window.StatusBar) {
        // org.apache.cordova.statusbar required
        StatusBar.styleDefault();
      }
      //延迟设置isVisible为false，防止第三方输入法返回退出当前页面
      window.addEventListener('native.keyboardhide', function (e) {
        cordova.plugins.Keyboard.isVisible = true;
        $timeout(function () {
          cordova.plugins.Keyboard.isVisible = false;
        }, 100);
      });
    });
    //双击退出
    $ionicPlatform.registerBackButtonAction(function (e) {
      if (cordova.plugins.Keyboard.isVisible) {
        cordova.plugins.Keyboard.close();
      }else {
        //判断处于哪个页面时双击退出
        if ($location.path() == '/login'||$location.path() == '/tab/workbench'||$location.path() == '/tab/tool'||$location.path() == '/tab/person') {
          if ($rootScope.backButtonPressedOnceToExit) {
            ionic.Platform.exitApp();
          } else {
            $rootScope.backButtonPressedOnceToExit = true;
            $cordovaToast.showShortTop('再按一次退出系统');
            $timeout(function () {
              $rootScope.backButtonPressedOnceToExit = false;
            }, 2000);
          }
        }else if ($ionicHistory.backView()) {
          $ionicHistory.goBack();
        } else {
          if ($rootScope.backButtonPressedOnceToExit) {
            ionic.Platform.exitApp();
          } else {
            $rootScope.backButtonPressedOnceToExit = true;
            $cordovaToast.showShortTop('再按一次退出系统');
            $timeout(function () {
              $rootScope.backButtonPressedOnceToExit = false;
            }, 2000);
          }
        }
      }
      e.preventDefault();
      return false;
    }, 101);
    //判断网络连接
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
      checkConnection();
    }
    function checkConnection() {
      var networkState = navigator.connection.type;

      var states = {};
      states[Connection.UNKNOWN]  = 'Unknown connection';
      states[Connection.ETHERNET] = 'Ethernet connection';
      states[Connection.WIFI]     = 'WiFi connection';
      states[Connection.CELL_2G]  = 'Cell 2G connection';
      states[Connection.CELL_3G]  = 'Cell 3G connection';
      states[Connection.CELL_4G]  = 'Cell 4G connection';
      states[Connection.CELL]     = 'Cell generic connection';
      states[Connection.NONE]     = 'No network connection';

      if(states[networkState]=='No network connection'){
        $state.go('login');
      }
    }
    if (Storage.get('isLogin')==1){
      ValidLogin.getValidLogin(JSON.stringify(Storage.get('user')));
      $rootScope.$on('ValidLogin.Updated', function() {
        var result=ValidLogin.getResult();
        if (result.success==true){
          $state.go('tab.workbench');
        }else {
          $state.go('login');
        }
      });
    }
    var posOptions = {timeout: 10000, enableHighAccuracy: false};
    $cordovaGeolocation
      .getCurrentPosition(posOptions)
      .then(function (position) {
        Storage.set('lat',position.coords.latitude);
        Storage.set('long',position.coords.longitude);
      }, function(err) {
        // error
      });
  });
